<?php
$page = 'admin';
include '../database/connect.php';
include_once '../actions/function.php';
$course_id = $_GET['course_id'];
if (isset($_GET['save'])) {
    session_start();
    extract($_POST);
    $added_by = $_SESSION['username'];
    $query = "INSERT INTO lessons SET course='$course', topic='$topic',added_by='$added_by'";
    mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?course_id=$course");
}
if (isset($_GET['delete'])) {
    session_start();
    extract($_POST);
    $tid = $_GET['lesson_id'];
    $query = "DELETE FROM lessons WHERE id='$tid'";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    header("Location: ?course_id=$course_id");
}
?>
<?php
require_once 'header.php';


$query = "SELECT * FROM courses where id='$course_id'";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
$row = mysql_fetch_assoc($result);
extract($row);
?>
<p>
    <a href="admin_home.php">Admin home</a>&gt;&gt;<a href='courses.php'>Courses</a>&gt;&gt;<?php echo $course; ?>
</p>
<h2 style="text-transform: capitalize">Course:  <?php echo $course; ?></h2>

<div id="edit_result"></div>
<div id="delete_result" style="display:none">
    <p class="error">Lesson deleted!</p>
</div>
<script type="text/javascript">
    initDialog("#edit_result","Edit lesson");
    initDialog("#delete_result","Lesson deleted");
            
    

    jQuery(document).ready(function(){
        jQuery("#lesson_form").validationEngine();
    });

    function editlessons(tid){
        $("#edit_result").hide();
        $("#loading").show();
        //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
        $("#edit_result").load("../actions/edit_lessons.php?id="+tid,function() {
            openDialog('#edit_result');
            $("#edit_result").show();
            $("#loading").hide();
        });
    }
            
  
    
    function deletelessons(tid){
        if(confirm("Are you sure you want to delete?")){
            $("#loading").show();
            //        prompt("", "/iboard_more/"+action.toLowerCase()+"/{{tabName}}/"+tid);
            $("#edit_result").load("../actions/delete_lessons.php?id="+tid,function() {
                openDialog('#delete_result');
                $("#loading").hide();
            });
            window.location="?";
        }
    }
</script>
<img src="../images/loading.gif" alt="Loading.." style="display: none;" id="loading"/>

<?php
$queryx = "SELECT count(*) as count1 FROM quizes where course_id='$course_id'";
$resultx = mysql_query($queryx) or die("Error executing '$queryx':" . mysql_error());
$row = mysql_fetch_assoc($resultx);
$count = $row['count1'];
?>
<a href="quizes.php?course_id=<?php echo $id ?>">Add/View Quizes (<?php echo $count; ?>)</a>

<form action="?save=1" method="POST" id="lesson_form">
    <input type="hidden" name="course" value="<?php echo $_GET['course_id']; ?>" />
    <table class="table">
        <tr>
            <td><input style="width: 99%" type="text" value="Enter a topic" class="validate[required] wide" name="topic" id='topic'/></td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: right;"><input type="submit" value="Save New Lesson"/></td>
        </tr>
    </table>
</form>
<?php
extract($_GET);
$username = $_SESSION['username'];
$query = "SELECT * FROM lessons where course='$course_id' order by 'date_added' desc";
$result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
if (mysql_num_rows($result) != 0) {
?>

    <table class="table" style="text-align: left">
        <tr>
            <th >
                Lesson Topic
            </th>
            <th >
                Date
            </th>
            <th >
                Edit
            </th>

            <th >Delete
            </th>
        </tr>
    <?php
    while ($row = mysql_fetch_assoc($result)) {
        extract($row);
//            var_dump($_GET);
    ?>
        <tr>

            <td >
                <a href="lessonsContent.php?lesson_id=<?php echo $id ?>"><?php echo $topic; ?></a>
            </td>
            <td >
            <?php echo normaliseDate($date_added); ?>
        </td>
        <td >
            <a href="javascript:editlessons(<?php echo $id ?>);"><img src="../images/edit.png" alt="Edit" title="Edit"/></a>
        </td>
        <td>
            <a href="?<?php echo "delete=1&lesson_id=$id&course_id=$course_id"; ?>"><img src="../images/delete.png" alt="Delete" title="Delete"/></a>
        </td>
    </tr>
    <?php } ?>
    </table>
    <div style="clear: both"></div>
<?php
    } else {
        echo "No lesson in the database";
    }
?>


<?php require_once('footer.php'); ?>